package test;

import java.io.*;
import java.util.*;
import java.sql.*;
import java.util.regex.*;

public class RegisterFormBean {

    public String fst;
    public String last;
    public String middle;
    public String ema;
    public String add;
    public String phn;
    public String mob;
    public String seq;
    public String sec;
    public String acc;
    public String atype;
    public String pass;
    public String cnfpass;
    public Hashtable errors;
    public DbConnect dbc;

    public boolean validate() {
        boolean bool = true;
        Pattern mobile = Pattern.compile("[0-9]*");
        Matcher m1 = mobile.matcher(mob);
        boolean b1 = m1.matches();
        Matcher m2 = mobile.matcher(phn);
        boolean b2 = m2.matches();

        System.out.println(b1);
        if (fst.equals("")) {
            errors.put("fst", "Please enter your first name");
            fst = "";
            bool = false;
        }
        if (last.equals("")) {
            errors.put("last", "Please enter your last name");
            last = "";
            bool = false;
        }
        if (middle.equals("")) {
            errors.put("middle", "Please enter your Middle name");
            middle = "";
            bool = false;
        }

        if (add.equals("")) {
            errors.put("add", "Please enter your address");
            add = "";
            bool = false;
        }
        if (phn.equals("")) {
            errors.put("phn", "Please enter your Phone Number");
            phn = "";
            bool = false;
        }
        if (!(mob.length() == 12) && !b1) {
            errors.put("mob", "Please enter 12 digit Mobile Number");
            mob = "";
            bool = false;
        }
        if (!b2) {
            errors.put("phn", "Please Enter Your Correct Landline Number");
            phn = "";
            bool = false;
        }
        if (mob.equals("")) {
            errors.put("mob", "Please Enter Your Mobile Number");
            mob = "";
            bool = false;
        }
        if (seq.equals("")) {
            errors.put("seq", "Please enter Security Question");
            seq = "";
            bool = false;
        }
        if (sec.equals("")) {
            errors.put("sec", "Please enter your security Anaswer");
            sec = "";
            bool = false;
        }
        if (acc.equals("")) {
            errors.put("acc", "Please enter Account Name");
            acc = "";
            bool = false;
        }
        if (atype.equals("")) {
            errors.put("atype", "Please enter Account type");
            atype = "";
            bool = false;
        }
        if (ema.equals("") || (ema.indexOf('@') == -1)) {
            errors.put("ema", "Please enter a valid email address");
            ema = "";
            bool = false;
        }
        if (!pass.equals("") && (cnfpass.equals("")
                || !pass.equals(cnfpass))) {
            errors.put("pass", "Please confirm your password");
            pass = "";
            bool = false;
        }

        return bool;
    }

    public String getErrorMsg(String s) {
        String errorMsg = (String) errors.get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }

    public RegisterFormBean() {
        fst = "";
        last = "";
        middle = "";
        ema = "";
        add = "";
        phn = "";
        mob = "";
        seq = "";
        sec = "";
        acc = "";
        atype = "";
        pass = "";
        cnfpass = "";
        errors = new Hashtable();
    }

    public String getFst() {
        return fst;
    }

    public String getLast() {
        return last;
    }

    public String getMiddle() {
        return middle;
    }

    public String getEma() {
        return ema;
    }

    public String getAdd() {
        return add;
    }

    public String getPhn() {
        return phn;
    }

    public String getMob() {
        return mob;
    }

    public String getSeq() {
        return seq;
    }

    public String getSec() {
        return sec;
    }

    public String getAcc() {
        return acc;
    }

    public String getAtype() {
        return atype;
    }

    public void setFst(String a) {

        fst = a;
    }

    public void setLast(String a) {

        last = a;
    }

    public void setMiddle(String b) {

        middle = b;
    }

    public void setEma(String b) {
        ema = b;
    }

    public void setAdd(String a) {

        add = a;
    }

    public void setPhn(String a) {

        phn = a;
    }

    public void setMob(String a) {

        mob = a;
    }

    public void setSeq(String a) {

        seq = a;
    }

    public void setSec(String a) {

        sec = a;
    }

    public void setAcc(String a) {

        acc = a;
    }

    public void setAtype(String a) {

        atype = a;
    }

    public void setPass(String a) {
        pass = a;
    }

    public void setCnfpass(String a) {
        cnfpass = a;
    }

    public boolean match() {

        boolean flag = false;
        Connection con = null;

        dbc = new DbConnect();
        String sequence1 = null;
        String sequence2 = null;
        String sequence3 = null;
        int ff, billstat, bal, defstat;
        try {

            con = dbc.getOracleConnection();
            Statement st = con.createStatement();
            PreparedStatement seq_ps = con.prepareStatement("select seq_custid.nextval,seq_accno.nextval,seq_bill.nextval from dual");
            ResultSet seq_rs = seq_ps.executeQuery();

            while (seq_rs.next()) {
                sequence1 = seq_rs.getString(1);
                sequence2 = seq_rs.getString(2);
                sequence3 = seq_rs.getString(3);
            }
            ff = 0;
            billstat = 0;
            bal = 10000;
            defstat = 1;
            st.executeUpdate("insert into onbill_customer values(" + "'" + sequence1 + "'" + "," + "'" + fst + "'," + "'" + last + "'" + "," + "'" + middle + "'" + "," + "'" + ema + "'" + "," + "'" + add + "'" + "," + "'" + phn + "'" + "," + "'" + mob + "'" + "," + "'" + pass + "'" + "," + "'" + ff + "'" + "," + "'" + seq + "'" + "," + "'" + sec + "'" + ")");
            st.executeUpdate("insert into onbill_billstatus values(" + "'" + sequence2 + "'" + "," + "'" + billstat + "'" + ")");
            st.executeUpdate("insert into onbill_account values(" + "'" + sequence1 + "'" + "," + "'" + sequence3 + "'," + "'" + acc + "'" + "," + "'" + atype + "'" + "," + "'" + bal + "'" + "," + "'" + defstat + "'" + "," + "'" + sequence2 + "'" + ")");




            flag = true;








        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }
}
